#pragma once

#include "base/core/singleton.h"
#include "Common/MailDefine.h"
#include "Common/MessageDispatch/IMessageDispatch.h"
#include "ProtoBuffer/AllProtocol.h"
#include "Common/CharacterDefine.h"
#include "Common/Timer/TimerAxis.h"
#include "Common/Event/Event.h"
#include "Common/EventDefine.h"
#include "Common/Item/Item.h"

class Player;

//逻辑服的邮件管理器
class GMailManager :	public IClientMessageHandler, 
						public IServerMessageHandler, 
						public IEventExecuteSink,
						public ITimerHandler,
						public IDBProxyMessageHandler,
						public ManualSingleton<GMailManager>
{
	enum
	{
		TIMER_ID_StartLoad_SysMailData = 0,
	};

	typedef std::map<string, SysMailInfo>	SysConfigMailMap;

public:
	bool 				Init();
	void 				UnInit();
	/////////////////////////////  IClientMessageHandler /////////////////////////////////////////////
	virtual void		onClientMessage(uint32_t actionId, RECV_PACKAGE& package, uint64_t Id = 0);
	virtual const char* getClientHandlerDebugInfo(void) { return "GMailManager::onClientMessage"; }
	//////////////////////////////  IServerMessageHandler ////////////////////////////////////////////
	virtual void		onServerMessage(uint32_t actionId, RECV_PACKAGE &package);
	virtual const char* getServerHandlerDebugInfo(void){ return "GMailManager::onServerMessage"; }
	//////////////////////////////////////////////////////////////////////////
	virtual void		onDBProxyMessage(uint32_t actionId, RECV_PACKAGE &package);
	virtual const char* getDBProxyHandlerDebugInfo(void){ return "GMailManager::onDBProxyMessage"; }

	virtual void		OnExecute(uint16_t nEventID, uint64_t nSrcID, uint8_t bySrcType, void *pContext, int32_t nLen);
	virtual void		OnTimer(uint32_t nTimerID);

	void				LoadSysMailDBRet(SysMailInfoLoadRsp& rsp);

public:
	/////////////////////////////  on client protocol //////////////////////////////////////

	////////////////////////////    on server protocol ////////////////////////////////////
	bool				OnSendMailReq(RECV_PACKAGE &package);
	bool				OnSendMailError(RECV_PACKAGE &package);
	bool				OnGetMailAttachment(RECV_PACKAGE &package);
	bool				OnGetAllMailAttachment(RECV_PACKAGE &package);

	void				OnLoginGame(LoginGame& loginGame);

public:
	ERetCode			SendNormalMail(Player* pPlayer, CWSendMailReq& sendMailReq);

	/*
		发送系统邮件：包括附件和货币，只有允许被发送附件或货币的才调用次接口 自动发送货币到玩家账户
		destId: 目标玩家cid
		title:邮件主题
		content：邮件内容
		gold：金币
		bindGold：绑定金币
		diamond：钻石
		bindDiamond：绑定钻石
		arenaScore：竞技场积分
		itemList：物品列表，注意这里是带物品属性的物品列表， 如果有附件,此接口会使用外部传进来的附件物品属性
	*/
	bool				SendSystemMail(CharIDType destId, string topic, string content, uint32_t gold, uint32_t bindGold, uint32_t diamond, uint32_t bindDiamond, uint32_t arenaScore, VEC_ITEM_PROTO_EX& itemList);

	/*
		发送系统邮件：包括附件和货币，只有允许被发送附件或货币的才调用次接口 自动发送货币到玩家账户
		destId: 目标玩家cid
		title:邮件主题
		content：邮件内容
		gold：金币
		bindGold：绑定金币
		diamond：钻石
		bindDiamond：绑定钻石
		arenaScore：竞技场积分
		lstItem：物品列表  此接口内会重新创建附件物品的全部属性,如果要发送的物品，外部已经确定了属性，则不允许调用这个接口
		如果有附件,这个接口用于只知道 物品ID,数量，绑定状态这些参数
	*/
	bool				SendSystemMail(CharIDType destId, string topic, string content, uint32_t gold, uint32_t bindGold, uint32_t diamond, uint32_t bindDiamond, uint32_t arenaScore, LIST_ITEM &lstItem);

public:
	GMailManager();
	virtual ~GMailManager();

private:
	SysConfigMailMap				_sysConfigMail;		//运营系统邮件配置
};

#define g_MakeGMailMgr() (GMailManager::Instance())
#define g_GetGMailMgr()  (GMailManager::GetInstance())
#define g_DelGMailMgr()  (GMailManager::Destroy())
